#include "stdafx.h"
#include <XComboBox.h>



XComboBox::XComboBox(XHash hash, XWindow* parent)
	: XWidget(hash, parent)
{
	Active = -1;
	SetExpanded(false);

	Render = new XRenderPack(Hash.String + "-Layout");
	Text = new XTextPack(Hash);
	Text->SetFont(ParentWindow->GetFont());

	parent->RegisterControlsRender(Render);
	parent->RegisterControlsText(Text);

	_loadVertices();
	_loadTextureVertices();
}

void XComboBox::SetDisable()
{
	Enabled = false;
	SetExpanded(false);

	Render->SetColor(XColor(255, 255, 255, 100));
	Text->SetColor(XColor(200, 200, 200, 100));
}

void XComboBox::_loadVertices()
{
	if(Render != NULL)
	{
		if(Expanded == false)
		{
			XVertex* vert = new XVertex[4];

			vert[0].X = Rectangle.X;
			vert[0].Y = Rectangle.Y;
			vert[1].X = Rectangle.X + Rectangle.W;
			vert[1].Y = Rectangle.Y;
			vert[2].X = Rectangle.X; 
			vert[2].Y = Rectangle.Y + Rectangle.H;
			vert[3].X = Rectangle.X + Rectangle.W;
			vert[3].Y = Rectangle.Y + Rectangle.H;

			Render->SetVertex(vert, 4);
			delete[] vert;
		}
		else
		{
			XVertex* vert = new XVertex[6];

			vert[0].X = Rectangle.X;
			vert[0].Y = Rectangle.Y;
			vert[1].X = Rectangle.X + Rectangle.W;
			vert[1].Y = Rectangle.Y;
			vert[2].X = Rectangle.X; 
			vert[2].Y = Rectangle.Y + Rectangle.H;
			vert[3].X = Rectangle.X + Rectangle.W;
			vert[3].Y = Rectangle.Y + Rectangle.H;
			vert[4].X = Rectangle.X;
			vert[4].Y = Rectangle.Y + Rectangle.H + Data.size()*Rectangle.H;
			vert[5].X = Rectangle.X + Rectangle.W;
			vert[5].Y = Rectangle.Y + Rectangle.H + Data.size()*Rectangle.H;

			Render->SetVertex(vert, 6);
			delete[] vert;
		}
	}
}

void XComboBox::_loadTextureVertices()
{
	if(Render != NULL)
	{
		if(Expanded == false)
		{
			XVertex* vert = new XVertex[4];

			vert[0].X = (float)512/ParentWindow->GetTexture()->GetWidth();
			vert[0].Y = (float)116/ParentWindow->GetTexture()->GetHeight();
			vert[1].X = (float)664/ParentWindow->GetTexture()->GetWidth();
			vert[1].Y = (float)116/ParentWindow->GetTexture()->GetHeight();
			vert[2].X = (float)512/ParentWindow->GetTexture()->GetWidth();
			vert[2].Y = (float)150/ParentWindow->GetTexture()->GetHeight();
			vert[3].X = (float)664/ParentWindow->GetTexture()->GetWidth();
			vert[3].Y = (float)150/ParentWindow->GetTexture()->GetHeight();

			Render->SetTexture(ParentWindow->GetTexture(), vert, 4);
			delete[] vert;
		}
		else
		{
			XVertex* vert = new XVertex[6];

			vert[0].X = (float)512/ParentWindow->GetTexture()->GetWidth();
			vert[0].Y = (float)116/ParentWindow->GetTexture()->GetHeight();
			vert[1].X = (float)664/ParentWindow->GetTexture()->GetWidth();
			vert[1].Y = (float)116/ParentWindow->GetTexture()->GetHeight();
			vert[2].X = (float)512/ParentWindow->GetTexture()->GetWidth();
			vert[2].Y = (float)150/ParentWindow->GetTexture()->GetHeight();
			vert[3].X = (float)664/ParentWindow->GetTexture()->GetWidth();
			vert[3].Y = (float)150/ParentWindow->GetTexture()->GetHeight();
			vert[4].X = (float)513/ParentWindow->GetTexture()->GetWidth();
			vert[4].Y = (float)254/ParentWindow->GetTexture()->GetHeight();
			vert[5].X = (float)664/ParentWindow->GetTexture()->GetWidth();
			vert[5].Y = (float)254/ParentWindow->GetTexture()->GetHeight();

			Render->SetTexture(ParentWindow->GetTexture(), vert, 6);
			delete[] vert;
		}
	}
}

void XComboBox::SetGeometry(XRectangle rect)
{
	Rectangle = rect;
	_loadVertices();

	Text->SetTextBox(XRectangle(Rectangle.X + 5, Rectangle.Y+5, Rectangle.W -5, Rectangle.H-5));
}

void XComboBox::SetExpanded(bool expanded)
{
	if(expanded == Expanded)
		return;

	if(expanded)
	{
		Expanded = true;
		_loadTextureVertices();
		_loadVertices();

		for(int i=0; i<Data.size(); i++)
		{
			Data[i]->Text->SetColor(XColor(255, 255, 255, 255));
		}
	}
	else
	{
		Expanded = false;
		_loadTextureVertices();
		_loadVertices();

		for(int i=0; i<Data.size(); i++)
		{
			Data[i]->Text->SetColor(XColor(255, 255, 255, 0));
		}
	}
}

void XComboBox::AddData(const char* text, void* data)
{
	XComboBoxData* comboData = new XComboBoxData;
	comboData->Data = data;

	comboData->Text = new XTextPack(Hash.String + text);
	comboData->Text->SetFont(ParentWindow->GetFont());
	comboData->Text->SetText(text);
	comboData->Text->SetTextBox(XRectangle(Rectangle.X + 2, Rectangle.Y +5 + Rectangle.H * (Data.size()+1), Rectangle.W-2, Rectangle.H-5));
	comboData->Text->SetColor(XColor(255, 255, 255, 0));
	ParentWindow->RegisterControlsText(comboData->Text);
	
	Data.push_back(comboData);
}

void* XComboBox::GetActiveData()
{
	return Data[Active]->Data;
}

void* XComboBox::GetData(int nr)
{
	return Data[nr]->Data;
}

void XComboBox::Update(XInputModule* input)
{
	if(input->GetMouse().GetLeft().GetCurrent() == true && input->GetMouse().GetLeft().GetPrevious() == false && Enabled == true)
	{
		XPosition pos = input->GetMouse().GetPosition();

		if(pos.Y > Rectangle.Y + Render->GetTranslation().Y && pos.Y < Rectangle.Y + Rectangle.H + Render->GetTranslation().Y)
		{
			if(pos.X > Rectangle.X + Render->GetTranslation().X + (115*Rectangle.W/152) && pos.X < Rectangle.X + Render->GetTranslation().X + Rectangle.W)
			{
				SetExpanded(!Expanded);
			}
		}
	}

	if(Expanded != true)
	{
		

	}
	else
	{
		for(int i =0; i<Data.size(); i++)
		{
			Data[i]->Text->SetTranslation(XVector(ParentWindow->GetGeometry()->X, ParentWindow->GetGeometry()->Y));

			
			if(input->GetMouse().GetLeft().GetCurrent() == true && input->GetMouse().GetLeft().GetPrevious() == false)
			{
				XPosition pos = input->GetMouse().GetPosition();

				if(pos.X > Data[i]->Text->GetTranslation().X + Data[i]->Text->GetTextBox().X && pos.X < Data[i]->Text->GetTranslation().X + Data[i]->Text->GetTextBox().X + Data[i]->Text->GetTextBox().W)
				{
					if(pos.Y > Data[i]->Text->GetTranslation().Y + Data[i]->Text->GetTextBox().Y && pos.Y < Data[i]->Text->GetTranslation().Y + Data[i]->Text->GetTextBox().Y + Data[i]->Text->GetTextBox().H)
					{
						Active = i;
						Text->SetText(Data[i]->Text->GetText());
						SetExpanded(false);
					}
				}
			}
		}
		


	}


	Render->SetTranslation(XVector(ParentWindow->GetGeometry()->X, ParentWindow->GetGeometry()->Y));
	Text->SetTranslation(XVector(ParentWindow->GetGeometry()->X, ParentWindow->GetGeometry()->Y));
}